package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.common;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.projectile.thrown.PotionEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.potion.PotionUtil;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;

import java.util.ArrayList;
import java.util.Collection;

public class AlchemySkill extends BaseSkill {

    public AlchemySkill(int level) {
        super(level);
    }

    public AlchemySkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public AlchemySkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setCooldown(60);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("炼金技能，可以发射药水。随等级提升伤害。冷却时间 %d tick。", skillConfig.getCooldown());
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.getTarget() != null && getSkilltime() < mobEntity.world.getTime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown());
            LivingEntity target = mobEntity.getTarget();
            Vec3d vec3d = target.getVelocity();
            Collection<StatusEffectInstance> statusEffectInstances = new ArrayList<>();
            if (target.isUndead()) {
                statusEffectInstances.add(new StatusEffectInstance(StatusEffects.INSTANT_HEALTH, getSkillLevel() / 3 + 1, getSkillLevel() - 1));
            } else {
                statusEffectInstances.add(new StatusEffectInstance(StatusEffects.INSTANT_DAMAGE, getSkillLevel() / 3 + 1, getSkillLevel() - 1));
            }
            PotionEntity potionEntity = new PotionEntity(mobEntity.world, mobEntity);
            potionEntity.setPos(potionEntity.getX(), potionEntity.getY() + 1, potionEntity.getZ());
            double d = target.getX() + vec3d.x - potionEntity.getX();
            double e = target.getEyeY() - 1.1 - potionEntity.getY();
            double f = target.getZ() + vec3d.z - potionEntity.getZ();
            float g = MathHelper.sqrt((float) (d * d + f * f));
            potionEntity.setItem(PotionUtil.setCustomPotionEffects(new ItemStack(Items.SPLASH_POTION), statusEffectInstances));
            potionEntity.pitch -= -20.0f;
            potionEntity.setVelocity(d, e + (double) (g * 0.2f), f, 0.75f, 8.0f);
            mobEntity.world.spawnEntity(potionEntity);
        }
        return true;
    }
}
